package com.cos.blog.controller.web;


import com.cos.blog.core.constants.BaseConstants;
import com.cos.blog.core.servlet.BaseServlet;
import com.cos.blog.entity.CommentEntity;
import com.cos.blog.entity.UserEntity;
import com.cos.blog.services.ICommentService;
import com.cos.blog.services.impl.CommentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import java.util.Map;

@WebServlet("/commentServlet/*")
public class CommentServlet extends BaseServlet {

    private ICommentService commentService = new CommentServiceImpl();

    public String add() throws Exception {

        UserEntity loginUser = (UserEntity) req.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            return outJson(100, "请先登录");
        }

        // 1.获取表单数据
        Map<String, String[]> parameterMap = req.getParameterMap();

        // 2.准备实体类
        CommentEntity commentEntity = new CommentEntity();

        // 3.把Map中的数据拷贝到实体类中
        BeanUtils.populate(commentEntity, parameterMap);

        // 4.入库
        commentEntity.setUserId(loginUser.getId());
        commentService.save(commentEntity);

        // 5.重定向当前帖子下面
        return BaseConstants.BASE_REDIRECT+":invitataionServlet/info?id="+commentEntity.getEntityId();
    }
}
